package cn.fan.whale.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.fan.whale.entity.dto.BaseReturnDto;
import cn.fan.whale.entity.po.RoleEntity;
import cn.fan.whale.entity.vo.DistributeRequest;
import cn.fan.whale.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@CrossOrigin
@SaCheckLogin
public class RoleController {

    private final RoleService roleService;

    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    @SaCheckPermission("system.role")
    @GetMapping("/findByPage")
    public BaseReturnDto findByPage(Integer pageNum, Integer pageSize, RoleEntity roleEntity) {
        PageInfo<RoleEntity> pageInfo = roleService.findByPage(pageNum, pageSize, roleEntity);
        return BaseReturnDto.success(pageInfo);
    }

    @GetMapping("/findAll")
    public BaseReturnDto findAll() {
        List<RoleEntity> roleEntityList = roleService.findAll();
        return BaseReturnDto.success(roleEntityList);
    }

    @SaCheckPermission("system.role:save")
    @PostMapping("/save")
    public BaseReturnDto save(@RequestBody RoleEntity roleEntity) {
        roleService.save(roleEntity);
        return BaseReturnDto.success(BaseReturnDto.RESP_SUCCESS_CODE, "操作成功");
    }

    @SaCheckPermission("system.role:update")
    @PostMapping("/update")
    public BaseReturnDto update(@RequestBody RoleEntity roleEntity) {
        roleService.update(roleEntity);
        return BaseReturnDto.success(BaseReturnDto.RESP_SUCCESS_CODE, "操作成功");
    }

    @SaCheckPermission("system.role:distribute")
    @PostMapping("/distribute")
    public BaseReturnDto distribute(@RequestBody DistributeRequest distributeRequest) {
        roleService.distribute(distributeRequest);
        return BaseReturnDto.success(BaseReturnDto.RESP_SUCCESS_CODE, "操作成功");
    }

    @SaCheckPermission("system.role:delete")
    @GetMapping("/delete")
    public BaseReturnDto delete(Integer roleId) {
        roleService.delete(roleId);
        return BaseReturnDto.success(BaseReturnDto.RESP_SUCCESS_CODE, "操作成功");
    }
}
